//
//  HomeController.swift
//  QualityControlSystem
//
//  Created by dundun on 17/1/12.
//  Copyright © 2017年 com.mask. All rights reserved.
//

import UIKit
import SVProgressHUD

class HomeController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = Color.bgGray
        tableView.separatorStyle = .none
        tableView.register(UINib.init(nibName: "HomeTopCell", bundle: nil), forCellReuseIdentifier: "homeTopCell")
        tableView.register(UINib.init(nibName: "HomeTitleCell", bundle: nil), forCellReuseIdentifier: "homeTitleCell")
        tableView.register(UINib.init(nibName: "HomeNoticeContentCell", bundle: nil), forCellReuseIdentifier: "homeNoticeContentCell")
        tableView.register(UINib.init(nibName: "HomeMattersTitleCell", bundle: nil), forCellReuseIdentifier: "homeMattersTitleCell")

        loadData()
    }

    func loadData() {
        
    }
    
    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        } else if section == 1 {
            return 2
        } else {
            return 4
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            //顶部
            let homeTopCell = tableView.dequeueReusableCell(withIdentifier: "homeTopCell", for: indexPath) as? HomeTopCell
            homeTopCell?.allApplicationBlock = { () in
                //点击所有应用
                if User.shared.auth_key == nil {
                    SVProgressHUD.show(withStatus: "验证权限...")
                    HTTPRequest.authQulityControlSystemWith(username: "hughruby", completion: { response in
                        if response.count == 0 {
                            SVProgressHUD.showError(withStatus: "无访问权限!")
                        } else {
                            SVProgressHUD.dismiss()
                            let user = User.shared
                            user.auth_key = response["auth_key"] as! String?
                            user.id = String(response["id"] as! Int)
                            user.is_push = response["is_push"] as! Int?
                            user.role = Role(rawValue: response["role"] as! Int)
                            user.archive()
                            let vc = UIStoryboard(name: "MainTabController", bundle: nil).instantiateInitialViewController() as? UITabBarController
                            self.present(vc!, animated: true, completion: nil)
                        }
                    })
                } else {
                    let vc = UIStoryboard(name: "MainTabController", bundle: nil).instantiateInitialViewController() as? UITabBarController
                    self.present(vc!, animated: true, completion: nil)
                }
                print("所有应用")
            }
            homeTopCell?.projectManagementBlock = { () in
                //点击项目管理
                
                print("项目管理")
            }
            homeTopCell?.mailSystemBlock = { () in
                //点击邮件系统
                let vc = ApplicationController()
                self.navigationController?.pushViewController(vc, animated: true)
                print("邮件系统")
            }
            homeTopCell?.officeSystemBlock = { () in
                //点击办公系统
                print("办公系统")
            }
            homeTopCell?.siteManagementBlock = { () in
                //点击站点管理
                print("站点管理")
            }
            homeTopCell?.projectProvidedBlock = { () in
                //点击项目填报
                print("项目填报")
            }
            
            return homeTopCell!
        } else if indexPath.section == 1 {
            if indexPath.row == 0 {
                //通知标题
                let homeNoticeContentTitleCell = tableView.dequeueReusableCell(withIdentifier: "homeTitleCell", for: indexPath) as? HomeTitleCell
                homeNoticeContentTitleCell?.homeTitleIconImage.image = UIImage(named: "notice")
                homeNoticeContentTitleCell?.homeTitle.text = "通知"
                return homeNoticeContentTitleCell!
            } else {
                //通知内容
                let homeNoticeContentCell = tableView.dequeueReusableCell(withIdentifier: "homeNoticeContentCell", for: indexPath) as? HomeNoticeContentCell
                homeNoticeContentCell?.noticeContentTitle.text = "111111111标题"
                homeNoticeContentCell?.noticeContent.text = "大概内容是萨德卡死的那家店里撒娇的垃圾是离开的拉伸件多拉拉萨的临界点"
                homeNoticeContentCell?.noticeTime.text = "2012-12-12"
                return homeNoticeContentCell!
            }
        } else {
            if indexPath.row == 0 {
                //待办事项标题
                let homeTitleCell = tableView.dequeueReusableCell(withIdentifier: "homeTitleCell", for: indexPath) as? HomeTitleCell
                homeTitleCell?.homeTitleIconImage.image = UIImage(named: "matters")
                homeTitleCell?.homeTitle.text = "待办事项"
                return homeTitleCell!
            } else {
                //待办事项内容
                let homeMattersTitleCell = tableView.dequeueReusableCell(withIdentifier: "homeMattersTitleCell", for: indexPath) as? HomeMattersTitleCell
                homeMattersTitleCell?.mattersTitle.text = "待办事项标题\(indexPath.row)"
                homeMattersTitleCell?.mattersTime.text = "2015-12-12"
                return homeMattersTitleCell!
            }
        }
    }
    
    // MARK: - Table view delegate
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 360
        } else if indexPath.section == 1 {
            if indexPath.row == 0 {
                return 40
            } else {
                return 120
            }
        } else {
            return 40
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0.1
        } else {
            return 10
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1 && indexPath.row == 0 {
            //点击通知栏
            print("通知")
        } else if indexPath.section == 2 && indexPath.row == 0 {
            //点击待办事项栏
            print("待办事项")
        }
    }
    
    override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
        if (indexPath.section == 1 && indexPath.row == 0) || (indexPath.section == 2 && indexPath.row == 0) {
            return true
        } else {
            return false
        }
    }
}
